from enum import Enum
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any
from datetime import datetime

def get_current_timestamp() -> str:
    """获取当前时间戳，格式为ISO 8601"""
    return datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")

class SQLTokenType(Enum):
    """SQL Token类型枚举"""
    KEYWORD = "KEYWORD"
    IDENTIFIER = "IDENTIFIER"
    PUNCTUATION = "PUNCTUATION"
    STRING = "STRING"
    NUMBER = "NUMBER"
    OPERATOR = "OPERATOR"
    COMMENT = "COMMENT"
    WHITESPACE = "WHITESPACE"

@dataclass
class SQLToken:
    """SQL Token数据类"""
    type: SQLTokenType
    value: str
    line: int
    column: int

@dataclass
class ColumnDefinition:
    """列定义"""
    name: str
    type: Dict[str, str]
    nullable: bool = True
    primary_key: bool = False
    auto_increment: bool = False
    comment: str = ""
    default_value: Optional[str] = None
    enum_options: Optional[Dict[str, str]] = None

@dataclass
class IndexDefinition:
    """索引定义"""
    name: str
    type: str
    columns: List[str]

@dataclass
class TableDefinition:
    """表定义"""
    name: str = ""
    comment: str = ""
    columns: List[ColumnDefinition] = None
    indexes: List[IndexDefinition] = None
    options: Dict[str, str] = None

    def __post_init__(self):
        """初始化后的处理"""
        if self.columns is None:
            self.columns = []
        if self.indexes is None:
            self.indexes = []
        if self.options is None:
            self.options = {}

@dataclass
class SQLParseMetadata:
    """SQL解析元数据"""
    source_file: str
    dialect: str = "mysql"
    timestamp: str = field(default_factory=get_current_timestamp)

@dataclass
class SQLParseTableInfo:
    """SQL解析表信息"""
    name: str
    comment: str = ""
    engine: str = "InnoDB"
    charset: str = "utf8mb4"
    collate: str = "utf8mb4_general_ci"
    indexes: List[Dict[str, Any]] = field(default_factory=list)

@dataclass
class SQLParseResult:
    """SQL解析结果

    符合标准输出格式的解析结果类,包含:
    - version: 版本号
    - metadata: 元数据信息
    - table: 表结构信息
    - columns: 列信息字典
    """
    version: str = "1.0"
    metadata: SQLParseMetadata = field(default_factory=lambda: SQLParseMetadata(source_file=""))
    table: SQLParseTableInfo = field(default_factory=lambda: SQLParseTableInfo(name=""))
    columns: Dict[str, Dict[str, Any]] = field(default_factory=dict)

    def set_source_file(self, file_name: str) -> None:
        """设置源文件名"""
        self.metadata.source_file = file_name

    def add_column(self, column: ColumnDefinition) -> None:
        """添加列信息"""
        self.columns[column.name] = {
            "name": column.name,
            "type": {
                "name": column.type["type"],
                "length": column.type.get("length")
            },
            "nullable": column.nullable,
            "auto_increment": column.auto_increment,
            "comment": column.comment,
            "default_value": column.default_value,
            "primary_key": column.primary_key
        }

    def add_index(self, index: IndexDefinition) -> None:
        """添加索引信息"""
        self.table.indexes.append({
            "name": index.name,
            "type": index.type,
            "columns": index.columns
        })

